<?php

App::uses('Presentation', 'Presentation');
App::uses('Sanitize', 'Utility');

class pst_generar_ploteo extends Presentation {

    public function getCoordenadas($params) {
        $where = "";
        if (!empty($params->codigo_trata)) {
            $where.= " AND codigo_trata = '" . $params->codigo_trata . "' ";
        }
        if (!empty($params->calle)) {
            $where.= " AND calle LIKE '%" . $params->calle . "%' ";
        }
        if (!empty($params->altura)) {
            $where.= " AND altura='" . $params->altura . "' ";
        }
        if (!empty($params->comuna)) {
            $where.= " AND comuna='" . $params->comuna . "' ";
        }
        if (!empty($params->barrio)) {
            $where.= " AND barrio='" . $params->barrio . "' ";
        }

        $db = ConnectionManager::getDataSource('default');
        $coordenadas = $db->Query("SELECT id, calle, altura, coordenadas FROM sub_subsidios 
                                   WHERE en_ruta = 'No' AND calle IS NOT NULL AND altura IS NOT NULL AND coordenadas IS NOT NULL " . $where);

        $data = array();
        foreach ($coordenadas as $coordenada) {
            $data[] = array(
                'id' => (int) $coordenada['sub_subsidios']['id'],
                'domicilio' => $coordenada['sub_subsidios']['calle'] . ' ' . $coordenada['sub_subsidios']['altura'],
                'coordenadas' => array_map("trim", explode(",", $coordenada['sub_subsidios']['coordenadas']))
            );
        }

        return $data;
    }

    public function confirmarRuta($params) {
        if (empty($params->centro) || empty($params->zoom) || empty($params->markers)) {
            return array('status' => 'EMPTY');
        }

        $centro = str_replace(array("(", ")"), array("", ""), $params->centro);

        $encargado = "";
        if (!empty($params->encargado)) {
            $encargado = Sanitize::escape($params->encargado);
        }

        $informacion = "";
        if (!empty($params->encargado)) {
            $informacion = Sanitize::escape($params->informacion);
        }

        $db = ConnectionManager::getDataSource('default');
        $db->begin();

        $query = $db->Query("INSERT INTO sub_rutas (fecha_carga, user_id, centro, zoom, encargado, informacion) 
                                            VALUES ('" . date("Y-m-d H:i:s") . "', " . $_SESSION['Auth']['User']['id'] . ", '" . $centro . "', " . $params->zoom . ", '" . trim($encargado) . "', '" . trim(nl2br($informacion)) . "')");

        $ruta_id = $db->lastInsertId();
        
        if ($query === false) {
            $db->rollback();
            return array('status' => 'ERROR 1');
        }

        foreach ($params->markers as $marker) {
            if (!is_numeric($marker)) {
                $db->rollback();
                return array('status' => 'ERROR 2');
            }
            $query1 = $db->Query("UPDATE sub_subsidios SET en_ruta = 'Si' WHERE id=" . $marker);
            $query2 = $db->Query("INSERT INTO sub_subsidios_rutas (subsidio_id, ruta_id) VALUES (" . $marker . ", " . $ruta_id . ")");

            if ($query1 === false || $query2 === false) {
                $db->rollback();
                return array('status' => 'ERROR 3');
            }
        }

        $db->commit();

        return array('status' => 'OK');
    }
}